//
//  JsLabel.m
//  JsUtilitySdk
//
//  Created by Gikki Ares on 2023/4/23.
//

#import "JsLabel.h"

@implementation JsLabel

- (void)setFrame:(CGRect)frame {
	[super setFrame:frame];
}

- (CGRect)frame {
	return [super frame];
}

/**
 修正anchorPoint不为默认(0.5,0.5)时,约束不正确的现象.
 现象:当设置不同长度字符时,label.frame.origin.x会变动.
 
 原因:
 1,设置不同长度字符串,会调用setCenter,这个点的值为anchorPoint为0.5,0.5时候的值.
 2,事实上现在的anchorPoint.x不为0.5
 
 解决方案:
 1,手动调整center值,具体的值,从约束中读取.
 
 //设置label.text时,AutoLayout引擎触发:
 #1	0x00000001c2d5eb64 in -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:] ()
 Autolayout是计算出view的Point然后设置center, 67.5,12 ,并没有调用setFrame方法来改变frame.
 anchpoint设置为不同时,计算出来的center是一样的,导致最终frame不一样.
 父子约束,firstIttem是自,second位父,可以反过来吗?
 */

- (void)setCenter:(CGPoint)center{
	//self的约束,包含宽高和兄弟节点的约束.
	__block CGPoint newPoint = center;
	if(self.layer.anchorPoint.x != 0.5) {
		NSArray <NSLayoutConstraint *> * superConstrains = self.superview.constraints;
		[superConstrains enumerateObjectsUsingBlock:^(NSLayoutConstraint * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
			if(obj.firstItem == self && obj.firstAttribute == NSLayoutAttributeLeading) {
				newPoint = CGPointMake(obj.constant,center.y);
				*stop = YES;
			}
		}];
	}
	[super setCenter:newPoint];
}

@end
